package org.example.consumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author 季舟
 * @Date 2024-09-09 17:12
 */
@RestController
public class TestController {
    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping(value = "/test")
    public String getUser() {
        ServiceInstance instance = null;
        List<ServiceInstance> instances = discoveryClient.getInstances("nacos-user-service");
        if (instances.size()>0){
            instance = instances.get(0);
        }
        String requestUrl = "http://"+instance.getHost()+":"+instance.getPort()+"/echo/hello";
        String response = restTemplate.getForObject(requestUrl, String.class);
        return response;
        //return restTemplate.getForObject("http://192.168.1.106:8201/echo/hello", String.class);
    }
}
